/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.middleware.quartz.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hydaodaorder.entity.HyDaodaOrder;
import org.jeecg.modules.wlhy.hydaodaorder.service.IHyDaodaOrderService;
import org.jeecg.modules.wlhy.report.ShippingNoteDaodaService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 道达--上报运单轨迹
 *
 * @author duanlikao
 * @created 2021/07/22
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class DaodaUploadJobService implements Job {
    private static final Logger LOGGER = LoggerFactory.getLogger(DaodaUploadJobService.class);


    @Autowired
    private IHyDaodaOrderService hyDaodaOrderService;

    @Autowired
    private ShippingNoteDaodaService shippingNoteDaodaService;

    public SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 道达--上报运单轨迹
     */
    //@Scheduled(cron = "0 0/30 * * * ?") //半小时执行一次
    //@Scheduled(cron = "0 01 3 * * ?")//每天的03:01:00执行
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if (systemConfig == null || !systemConfig.getDkDaodaIzUse()) {
            LOGGER.info("司机代开-道达服务暂未开启,请联系管理员。");
            return;
        }

        QueryWrapper<HyDaodaOrder> queryWrapper = new QueryWrapper();
        queryWrapper.ne("upload_status", "1");
        queryWrapper.orderByDesc("create_time");

        List<HyDaodaOrder> daodaOrders = hyDaodaOrderService.list(queryWrapper);
        if (daodaOrders.size() == 0) {
            LOGGER.info("DaodaUploadJobService 无数据处理");
            return;
        }
        Long start = System.currentTimeMillis();
        LOGGER.info("DaodaUploadJobService uploadDaodaOrderLine:执行运单轨迹数据开始" + now.format(new Date()));
        for (HyDaodaOrder daodaOrder : daodaOrders) {
            try {
                HyShippingNote hyShippingNote = new HyShippingNote();
                hyShippingNote.setId(daodaOrder.getShippingnoteId());
                shippingNoteDaodaService.uploadDaodaOrderLine(
                        hyShippingNote
                        //shippingNoteService.getById(daodaOrder.getShippingnoteId())
                );
            } catch (Exception e) {
                LOGGER.info("DaodaUploadJobService uploadDaodaOrderLine:Exception : {}", e.getMessage());
                continue;
            }
        }
        LOGGER.info("DaodaUploadJobService uploadDaodaOrderLine:执行运单轨迹数据结束,耗时:" + (System.currentTimeMillis() - start) + "毫秒----");
    }

}
